# 1、导包
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium.webdriver import ActionChains, Keys

# 2、实例化浏览器对象
driver_path = "C:/Users/Lenovo/AppData/Local/Programs/Python/Python310/geckodriver.exe"
service = Service(executable_path=driver_path)
driver = webdriver.Firefox(service=service)

# TODO 鼠标操作
# 3、需求

# ① 在用户名文本框上点击鼠标右键
driver.get("file:///E:/project/learn-python/html%E5%9F%BA%E7%A1%80/3-%E8%A1%A8%E5%8D%95%E6%8F%90%E4%BA%A4.html")
username = driver.find_element(By.ID, "user")     # 1>  定位目标元素
action = ActionChains(driver)                     # 2>  实例化鼠标对象（关联浏览器对象）
action.context_click(username)                    # 3>  调用鼠标方法
action.perform()                                  # 4>  执行方法

# ② 输入用户名admin，暂停3秒后，双击鼠标左键，选中admin
username = driver.find_element(By.ID, "user")  # 1>  定位目标元素
username.send_keys("admin")
action = ActionChains(driver)                  # 2>  实例化鼠标对象
time.sleep(3)
action.double_click(username)                  # 3>  调用鼠标方法
action.perform()                               # 4>  执行方法

# ③ 把红色方框拖拽到蓝色方框上
driver.get("file:///E:/project/learn-python/html%E5%9F%BA%E7%A1%80/drag.html")
red = driver.find_element(By.ID, "red-box")    # 1>  定位目标元素
blue = driver.find_element(By.ID, "blue-box")
action = ActionChains(driver)                  # 2>  实例化鼠标对象
action.drag_and_drop(red, blue)                # 3>  调用鼠标方法
action.perform()                               # 4>  执行方法

# ④ 鼠标悬停在"注册"按钮上
driver.get("file:///E:/project/learn-python/html%E5%9F%BA%E7%A1%80/3-%E8%A1%A8%E5%8D%95%E6%8F%90%E4%BA%A4.html")
btn = driver.find_element(By.TAG_NAME, "button")     # 1>  定位目标元素
action = ActionChains(driver)                        # 2>  实例化鼠标对象
action.move_to_element(btn)                          # 3>  调用鼠标方法
action.perform()                                     # 4>  执行方法

# 另一种写法：链式编程
ActionChains(driver).move_to_element(btn).perform()

# time.sleep(4)
# driver.quit()


# TODO 键盘操作
# 需求：
# ① 输入用户名：admin1，暂停2秒，删除1
username = driver.find_element(By.ID, "user")
username.send_keys("admin1")
time.sleep(2)
username.send_keys(Keys.BACK_SPACE)

# ② 全选用户名：admin，暂停2秒
username.send_keys(Keys.CONTROL, 'a')
time.sleep(2)

# ③ 复制用户名：admin，暂停2秒
username.send_keys(Keys.CONTROL, 'c')
time.sleep(2)

# ④ 粘贴到密码框
password = driver.find_element(By.ID, 'pwd')
password.send_keys(Keys.CONTROL, 'v')